﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using Eaglight.Extentions;

namespace Eaglight
{
    public partial class MapServiceLayerToc : UserControl
    {
        private ArcGISDynamicMapServiceLayer lyr;
        
        public MapServiceLayerToc()
        {
            InitializeComponent();
        }

        private void initVisibileLayers()
        {
            List<int> visibleLayers = new List<int>();
            foreach (LayerInfo layerInfo in lyr.Layers)
            {
                if (layerInfo.DefaultVisibility)
                {
                    visibleLayers.Add(layerInfo.ID);
                }
            }
            lyr.VisibleLayers = visibleLayers.ToArray();
        }

        private void initLayers()
        {
            initVisibileLayers();
            lstMain.ItemsSource = lyr.Layers;
        }

        private void chkVisibile_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            int id = (int)cb.Tag;
            List<int> visibleLayers = lyr.VisibleLayers.ToList();
            if (!visibleLayers.Contains(id))
            {
                visibleLayers.Add(id);
            }
            lyr.VisibleLayers = visibleLayers.ToArray();
        }

        private void chkVisibile_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            int id = (int)cb.Tag;
            List<int> visibleLayers = lyr.VisibleLayers.ToList();
            if (visibleLayers.Contains(id))
            {
                visibleLayers.Remove(id);
            }
            lyr.VisibleLayers = visibleLayers.ToArray();
        }

        public ArcGISDynamicMapServiceLayer MapServiceLayer
        {
            get 
            { 
                return lyr; 
            }
            set 
            { 
                lyr = value;
            }
        }

        public void Refresh()
        {
            initLayers();
        }


       
    }
}
